<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Models\GoodsSpecs;
use App\Services\GoodsSpecsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsSpecsController extends AdminController
{
    //保存
    public function save(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'name' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method', '_token']);
        $id = $request->input('id');
        $flight = GoodsSpecs::updateOrCreate(
            ['id' => $id],
            $putData
        );
        return $this->success($flight->id);
    }
    //列表
    public function  list(Request $request){
        $validator = Validator::make($request->all(), [
            'name' => 'string',
            'page' => 'integer',
            'limit' => 'integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $name = $request->input('name');
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);

        $list = GoodsSpecsService::getInstance()->list($page,$limit,$name);
        return $this->success($list);
    }
    //列表
    public function  listAll(Request $request){
        $name = $request->input('name');
        $list = GoodsSpecsService::getInstance()->listAll($name);
        return $this->success($list);
    }
    //删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $expert = GoodsSpecs::find($request->id);
        $expert->delete();
        return $this->success();
    }
}
